package com.pzhu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

public class LogOutServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session =req.getSession();
        if (session.getAttribute("login") == null) {
            req.getRequestDispatcher("/main").forward(req, resp);
            return;
        }
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie:
             cookies) {
            System.out.println(cookie.getName()+":"+cookie.getValue());
            if ("account".equals(cookie.getName())){
                cookie.setMaxAge(0);
            }
        }
//        ||"Uname".equals(cookie.getName() )||"login".equals(cookie.getName())
        session.removeAttribute("login");
        session.removeAttribute("Uid");
        session.removeAttribute("Uname");
        session.setMaxInactiveInterval(0);
        System.out.println("退出登录");
        req.getRequestDispatcher("main").forward(req, resp);
        return;
    }
}
